<?php
/**
 * SmsService.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-18 01:02:43
 * @modified    2023-02-18 01:02:43
 *
 */

namespace App\Services;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use App\Library\Logger;
use Darabonba\OpenApi\Models\Config;

class SmsService extends BaseService
{
    private $logger;
    private $accessKeyId = 'LTAI5t5ufshfyAEyXQYaXJ4e';
    private $accessKeySecret = 'TGy6VJHg0s9hxRvFm3hfPPhbyfBGCB';
    private $signature = '嘉盛环境';
    private $endpoint = 'dysmsapi.aliyuncs.com';

    private $client;
    private $telephone;

    public function __construct($telephone)
    {
        $this->telephone = $telephone;

        $config = new Config([
            'accessKeyId' => $this->accessKeyId,
            'accessKeySecret' => $this->accessKeySecret
        ]);
        $config->endpoint = $this->endpoint;
        $this->client = new Dysmsapi($config);
        $this->logger = new Logger('sms_ali');
    }

    public function sendMessage($templateCode, $originParams = [])
    {
        $paramsArr = $originParams;
        $param = json_encode($paramsArr);
        $this->logger->info('发送短信验证码，短信code: ' . $templateCode . ', 参数: ' . $param);
        $requestParams = [
            'phoneNumbers' => $this->telephone,
            'signName' => $this->signature,
            'templateCode' => $templateCode
        ];
        if ($originParams) {
            $requestParams['templateParam'] = $param;
        }
        $this->logger->info("请求参数: ");
        $this->logger->info($requestParams);
        $smsRequest = new SendSmsRequest($requestParams);

        try {
            $response = $this->client->sendSmsWithOptions($smsRequest, new RuntimeOptions());
            $body = $response->body;
            $this->logger->info("响应结果: ");
            $this->logger->info($body);
            $responseCode = $body->code;
            if ($responseCode != 'OK') {
                throw new \Exception($body->message);
            }
            $this->logger->info("发送成功");
        } catch (\Exception $e) {
            $this->logger->error("发送失败");
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 发送验证码
     *
     * @param $code
     * @throws \Exception
     */
    public function sendVerifyCode($code)
    {
        $templateCode = 'SMS_463645433';
        $paramsArr = [
            'code' => $code
        ];
        $param = json_encode($paramsArr);
        $this->logger->info('发送短信验证码，短信code: ' . $templateCode . ', 参数: ' . $param);
        $requestParams = [
            'phoneNumbers' => $this->telephone,
            'signName' => $this->signature,
            'templateCode' => $templateCode,
            'templateParam' => $param
        ];
        $this->logger->info("请求参数: ");
        $this->logger->info($requestParams);
        $smsRequest = new SendSmsRequest($requestParams);

        try {
            $response = $this->client->sendSmsWithOptions($smsRequest, new RuntimeOptions());
            $body = $response->body;
            $this->logger->info("响应结果: ");
            $this->logger->info($body);
            $responseCode = $body->code;
            if ($responseCode != 'OK') {
                throw new \Exception($body->message);
            }
        } catch (\Exception $e) {
            $this->logger->error("发送失败");
            throw new \Exception($e->getMessage());
        }
    }
}